package com.example.project_01

import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import android.widget.ScrollView

class ScrollActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_scroll)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 获取ScrollView和返回顶部按钮
        val scrollView = findViewById<ScrollView>(R.id.scroll_view)
        val backToTopButton = findViewById<Button>(R.id.back_to_top_button)

        // 设置ScrollView滚动监听器
        scrollView.viewTreeObserver.addOnScrollChangedListener {
            val scrollY = scrollView.scrollY
            // 当滚动超过300像素时显示按钮，否则隐藏
            backToTopButton.visibility = if (scrollY > 300) View.VISIBLE else View.GONE
        }

        // 设置返回顶部按钮点击事件
        backToTopButton.setOnClickListener {
            scrollView.smoothScrollTo(0, 0) // 平滑滚动到顶部
        }
    }
}